package com.keson.spider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Comparator;

public class FileUtil {
    // 复制文件
    public static void copyFile(File sourceFile, File targetFile){
        // 新建文件输入流并对它进行缓冲
        try{
            InputStream input = new FileInputStream(sourceFile);
            BufferedInputStream inBuff=new BufferedInputStream(input);

            // 新建文件输出流并对它进行缓冲
            OutputStream output = new FileOutputStream(targetFile);
            BufferedOutputStream outBuff=new BufferedOutputStream(output);

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len =inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();

            //关闭流
            inBuff.close();
            outBuff.close();
            output.close();
            input.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    // 复制文件夹
    public static void copyDirectiory(String sourceDir, String targetDir){
        // 新建目标目录
    	new File(targetDir).getParentFile().mkdirs();
        // 获取源文件夹当前下的文件或目录
        File[] file = (new File(sourceDir)).listFiles();
        if(file==null){
            return;
        }
        for (int i = 0; i < file.length; i++) {
            if (file[i].isFile()) {
                // 源文件
                File sourceFile=file[i];
                // 目标文件
                File targetFile=new
                        File(new File(targetDir).getAbsolutePath()
                        +File.separator+file[i].getName());
                copyFile(sourceFile,targetFile);
            }
            if (file[i].isDirectory()) {
                // 准备复制的源文件夹
                String dir1=sourceDir + "/" + file[i].getName();
                // 准备复制的目标文件夹
                String dir2=targetDir + "/"+ file[i].getName();
                copyDirectiory(dir1, dir2);
            }
        }
    }

//    递减排序
    public static File[] sortDown(File[] files){
        Arrays.sort(files, new Comparator<File>() {
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return -1;
                else if (diff == 0)
                    return 0;
                else
                    return 1;//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
            }

            public boolean equals(Object obj) {
                return true;
            }
        });
        return files;
    }

    //    递减排序
    public static File[] sortDownByFileName(File[] files){
        Arrays.sort(files, new Comparator<File>() {
            public int compare(File f1, File f2) {
                    return f1.getName().compareTo(f2.getName());//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
            }

            public boolean equals(Object obj) {
                return true;
            }
        });
        return files;
    }

    //    递增排序
    public static File[] sortUpByFileName(File[] files){
        Arrays.sort(files, new Comparator<File>() {
            public int compare(File f1, File f2) {
                return f2.getName().compareTo(f1.getName());//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
            }

            public boolean equals(Object obj) {
                return true;
            }
        });
        return files;
    }

//递增排序
    public static File[] sortUp(File[] files){
        Arrays.sort(files, new Comparator<File>() {
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return 1;
                else if (diff == 0)
                    return 0;
                else
                    return -1;//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
            }

            public boolean equals(Object obj) {
                return true;
            }
        });
        return files;
    }

//    获取文件夹大小 单位为B
    public static long getFolderSize(File file) {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            if(fileList!=null){
                for (int i = 0; i < fileList.length; i++) {
                    if (fileList[i].isDirectory()) size = size + getFolderSize(fileList[i]);
                    else size = size + fileList[i].length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    public static String readFileContent(File file){

        FileReader is = null;
        BufferedReader br = null;

        String str = "";
        try {
            is = new FileReader(file);
            br = new BufferedReader(is);

            String line = "";
            while((line = br.readLine()) != null) {
                str = str + line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static void writeFileContent(File file, String content){
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(content.getBytes());

        } catch (IOException ex) {
        } finally {
            try {
                os.close();
            } catch (IOException ex1) {
            }
        }
    }

    public static void deleteDir(String dirPath) {
        File file = new File(dirPath);
        if(file.isFile()){
            file.delete();
        }else{
            File[] files = file.listFiles();
            if(files == null){
                file.delete();
            }else{
                for (int i = 0; i < files.length; i++){
                    deleteDir(files[i].getAbsolutePath());
                }
                file.delete();
            }
        }
    }
}
